<?php
/**
 * Created by Teacher黄.
 * User: Administrator
 * Date: 2017/10/25
 * Time: 16:37
 */

namespace app\index\controller;


use app\common\controller\Base;
use app\index\model\Access;
use app\index\model\Admin;
use app\index\model\AdminRole;
use app\index\model\Role;
use app\index\model\RoleAccess;
use think\Request;

class Acl extends Base
{
    # 用户列表
    public function user(){
        # 用户列表
        $admin = new Admin();
        $res = $admin->order('admin_id desc')->select();
        $this->assign('list',$res);

        return $this->fetch();
    }

    # 获取角色
    private function getRole(){
        $role = new Role();
        $list = $role->order('role_id desc')->select();
        $this->assign('roles',$list);
    }


    # 获取权限
    private function getAccess(){
        $model = new Access();
        $list = $model->order('access_id desc')->select();
        $this->assign('access',$list);
    }


    # 添加用户
    public function addUser(Request $request){
        if($request->isGet()){
            $this->getRole(); # 查询角色
            return $this->fetch();
        }elseif($request->isPost()){
            # 添加用户操作
            $admin = new Admin();
            $admin->admin_name = $request->post('admin_name');
            $admin->admin_pwd = $request->post('admin_pwd');
            $res = $admin->save();
            if($res){
                $this->success('添加用户成功!','acl/user');
            }else{
                $this->error('添加用户失败');
            }
        }

    }


    # 编辑用户
    public function editUser(Request $request){
        if($request->isGet()){
            # 获取一个id
            $id = $request->param('id');
            # 以id为条件去查询数据
            $admin = Admin::get($id);
            $this->assign('admin',$admin);
            # 根据id去获取用户的角色
            $ar = new AdminRole();
            $res = $ar->field('ar_role_id')->where(['ar_admin_id'=>$id])->select();
            $admin_role_ids = [];
            foreach ($res as $role){
                $admin_role_ids[] = $role->ar_role_id;
            }
            $this->assign('role_ids',$admin_role_ids);

            $this->getRole(); # 查询角色
            return $this->fetch();
        }elseif($request->isPost()){
            # 获取一个id
            $id = $request->post('id');
            # 添加用户操作
            $admin = Admin::get($id);
            $admin->admin_name = $request->post('admin_name');
            $res = $admin->save();
            # 更新用户角色关系
            $post = $request->post();
            $roles = $post['roles'] ?? [];

            # 先删除之前的关系
            AdminRole::destroy(['ar_admin_id' => $id]);

            # 再向关系表中添加数据

            $ar = new AdminRole();
            $list = [];
            foreach ($roles as $v){
                $list[] = ['ar_admin_id' => $id,'ar_role_id' => $v];
            }
            $ar->saveAll($list);


            if($res !== false){
                $this->success('编辑用户成功!','acl/user');
            }else{
                $this->error('编辑用户失败');
            }
        }
    }

    # 角色列表
    public function role(){
        # 角色列表
        $role = new Role();
        $res = $role->order('role_id desc')->select();
        $this->assign('list',$res);
        return $this->fetch();

    }


    # 添加角色
    public function addRole(Request $request){
        if($request->isGet()){
            # 去查询所有的权限
            $this->getAccess();
            return $this->fetch();
        }elseif($request->isPost()){
            # 添加用户操作
            $modle = new Role();
            $modle->role_name = $request->post('role_name');
            $res = $modle->save();
            if($res){
                $this->success('添加角色成功!','acl/role');
            }else{
                $this->error('添加角色失败');
            }
        }

    }

    # 编辑角色
    public function editRole(Request $request){
        if($request->isGet()){
            # 获取一个id
            $id = $request->param('id');
            # 以id为条件去查询数据
            $role = Role::get($id);
            $this->assign('role',$role);
            # 去查询所有的权限
            $this->getAccess();
            # 根据id去获取角色的所有权限
            $ra = new RoleAccess();
            $res = $ra->field('ra_access_id')->where(['ra_role_id'=>$id])->select();
            $role_access_ids = [];
            foreach ($res as $access){
                $role_access_ids[] = $access->ra_access_id;
            }
            $this->assign('access_ids',$role_access_ids);
            return $this->fetch();
        }elseif($request->isPost()){
            # 获取一个id
            $id = $request->post('id');
            # 编辑角色操作
            $role = Role::get($id);
            $role->role_name = $request->post('role_name');
            $res = $role->save();
            # 更新角色权限关系
            $post = $request->post();
            $access = $post['access'] ?? [];

            # 先删除之前的关系
            RoleAccess::destroy(['ra_role_id' => $id]);

            # 再向关系表中添加数据

            $ra = new RoleAccess();
            $list = [];
            foreach ($access as $v){
                $list[] = ['ra_role_id' => $id,'ra_access_id' => $v];
            }
            $ra->saveAll($list);


            if($res !== false){
                $this->success('编辑角色成功!','acl/role');
            }else{
                $this->error('编辑角色失败');
            }
        }
    }


    # 权限列表
    public function access(){
        # 权限列表
        $access = new Access();
        $res = $access->order('access_id desc')->select();
        $this->assign('list',$res);
        return $this->fetch();
    }

    # 添加权限
    public function addAccess(Request $request){
        if($request->isGet()){
            return $this->fetch();
        }elseif($request->isPost()){
            # 添加用户操作
            $modle = new Access();
            $modle->access_name = $request->post('access_name');
            $modle->access_url = $request->post('access_url');
            $res = $modle->save();
            if($res){
                $this->success('添加权限成功!','acl/access');
            }else{
                $this->error('添加权限失败');
            }
        }

    }


}